import {Component, OnInit} from '@angular/core';
import {AddOrdOrderLogisticsComponent} from '../add-ord-order-logistics.component';
import {ErrRes} from '../../../../../frame/global/err-res';
import {OrdOrderLogistics} from '../../ord-order-logistics.type';
import {Services} from '../../../../../frame/module/services';
import {HttpRequestClient} from '../../../../../frame/http-request-client/http-request-client';
import {UrlRou} from '../../../../../frame';
import {Zero3Alert, Zero3AlertComponent} from '../../../../../frame/global/zero3-alert/zero3-alert.component';
import {Zero3Enum} from '../../../../../frame/enum/zero3-enum';
import * as fa from '@fortawesome/free-solid-svg-icons';
import {FroalaEditor} from '../../../../../frame/global/froala-editor';
import { isNullOrUndefined } from 'util';
import { ActivatedRoute } from '@angular/router';
import { OrdOrderCondition, OrdOrder } from '../../../ord-order/ord-order.type';

@Component({
    selector: 'app-main-add-ord-order-logistics',
    templateUrl: './main-add-ord-order-logistics.component.html',
    styleUrls: ['./main-add-ord-order-logistics.component.css']
})
export class MainAddOrdOrderLogisticsComponent implements OnInit {

    froalaEditorOptions = Object.assign(FroalaEditor.options, {});

    errRes = new ErrRes();

    domain = new OrdOrderLogistics();
    orderDomain: OrdOrder;

    extend = {
        domain: {
            logistics: {
                items: [
                    {key: 'shunfeng', value: '顺丰公司'}
                ],
                nickname: '请选择物流公司',

                change: (event) => {
                    if (isNullOrUndefined(event)) {
                        this.domain.logisticsCompany = '';
                        this.domain.logisticsCompanyPinyin = '';
                    } else {
                        this.domain.logisticsCompany = event.value;
                    }

                }
            },
            disabledBtn: () => {
                if (isNullOrUndefined(this.orderDomain)) {
                    return true;
                }
                return this.orderDomain['serOrderStatus'] !== 2;
            },

            btnName: () => {
                if (isNullOrUndefined(this.orderDomain)) {
                    return '发货';
                } else {
                    if (this.orderDomain['serOrderStatus'] === 2) {
                        return '发货';
                    } else {
                        return Zero3Enum.ORDER_STATUS_FLAG.get(this.orderDomain['serOrderStatus']);
                    }
                }
            }
        },
    };

    func = {
        loadDomain: (uuid: string) => {
            const condition: OrdOrderCondition = new OrdOrderCondition();
            condition.uuid = uuid;
            Services.ordOrder.info(condition).subscribe(item => {
                if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                    this.orderDomain = item.domain;
                    console.log(item.domain);
                    if (this.orderDomain['serOrderStatus'] !== 2) {
                        Services.ordOrderLogistics.getByOrderUUID(uuid).subscribe(logistics => {
                            if (!isNullOrUndefined(item) && !isNullOrUndefined(item.domain)) {
                                this.domain = logistics.domain;
                            }
                        });
                    }
                }
            });
        },
        save: {
            req: (event) => {
                console.log(this.orderDomain);
                this.domain.addressDetail = this.orderDomain.addressDetail;
                this.domain.addressStr = this.orderDomain.addressStr;
                this.domain.receiverName = this.orderDomain.receiverName;
                this.domain.receiverPhone = this.orderDomain.receiverPhone;
                this.domain.serOrderNo = this.orderDomain.serOrderNo;
                Services.ordOrderLogistics.save(this.domain, this.func.save.callback).subscribe();
            },
            callback: {
                success: (result) => {
                    /**
                     * moduleId: module.id, menuId: module.menu.id
                     * {moduleId: module.id, menuId: module.menu.id, uuid: condition.uuidList[0]}
                     */
                    const module = this.route.snapshot.paramMap.get('moduleId');
                    const menu = this.route.snapshot.paramMap.get('menuId');
                    HttpRequestClient.router.navigate([UrlRou.ordOrder.list, {moduleId: module, menuId: menu}]);
                },
                error: (result) => {
                    this.errRes.error = result.error;
                    this.errRes.statusText = result.statusText;
                }
            }
        }
    };

    constructor(
        public addOrdOrderLogistics: AddOrdOrderLogisticsComponent,
        private route: ActivatedRoute) {
        this.addOrdOrderLogistics.main = this;
    }

    ngOnInit() {
        this.domain.logisticsCompanyPinyin = 'shunfeng';
        this.domain.logisticsCompany = '顺丰公司';
        // uuid
        const uuid = this.route.snapshot.paramMap.get('uuid');
        this.domain.orderUuid = uuid;

        this.func.loadDomain(uuid);
    }


}
